package com.wangzhen.spring.inject;

import com.wangzhen.spring.inject.holder.UserHolder;
import com.wangzhen.spring.ioc.dependcy.entity.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * Aware接口回调注入方式演示
 */
public class AwareInterfaceInjectDemo implements BeanFactoryAware, ApplicationContextAware {

    private BeanFactory beanFactory;
    private ApplicationContext applicationContext;

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(AwareInterfaceInjectDemo.class);
        // 启动应用上下文
        context.refresh();
        // 获取AwareInterfaceInjectDemo
        AwareInterfaceInjectDemo demo = context.getBean(AwareInterfaceInjectDemo.class);

        System.out.println(demo.beanFactory == context.getBeanFactory());
        System.out.println(demo.applicationContext == context);
        System.out.println(demo.beanFactory == context);
        System.out.println(demo);
        // 关闭应用上下文
        context.close();

    }

}
